package TankGame;

import java.util.Random;
import java.util.Vector;

/**
 * @author 王艺锡
 * @version 1.0
 */
@SuppressWarnings({"all"})
public class EnemyTank extends Tank implements Runnable {
    Vector<Shot> shots = new Vector<>();
    Vector<EnemyTank> enemyTanks = new Vector<EnemyTank>();
    Shot shot = null;
    boolean isLive = true;
    Random random = new Random();


    //接收MyPanel中的 enemyTanks集合
    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public EnemyTank(int x, int y, int direct) {
        super(x, y, direct);
    }

    //开启一个线程，让地方坦克能够自由的移动起来
    @Override
    public void run() {

        while (true) {
            if (isLive && shots.size() < 5) {
                switch (getDirect()) {
                    case 0:
                        shot = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        shot = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:
                        shot = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:
                        shot = new Shot(getX(), getY() + 20, 3);
                        break;
                }
                shots.add(shot);
                new Thread(shot).start();
            }

            switch (getDirect()) {
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if (getY() > 0 && !isTouch()) {
                            moveUp();
                        }

                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if (getX() + 60 < 1000 && !isTouch()) {
                            moveRight();
                        }

                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if (getY() + 60 < 750 && !isTouch()) {
                            moveDown();
                        }

                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0 && !isTouch()) {
                            moveLeft();
                        }

                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }

            //随机生成0-3的整数，用于改变坦克方向
            int dir = random.nextInt(4);
            setDirect(dir);

            //如果坦克死亡，则退出线程
            if (isLive == false) {
                break;
            }
        }

    }

    //编写方法，防止地方坦克重叠
    public boolean isTouch() {
        //判断当前坦克的方向
        switch (this.getDirect()) {
            case 0://上
                //遍历所有坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //如果不是当前坦克,则判断
                    if (this != enemyTank) {
                        //如果另一个坦克的方向是上或者下
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //判断当前坦克的左上角是否落在另一个坦克的区域
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }

                            //判断当前坦克的右上角是否落在另一个坦克的区域
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }

                        //如果另一个坦克的方向是右或者左
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //判断当前坦克的左上角是否落在另一个坦克的区域
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }

                            //判断当前坦克的右上角是否落在另一个坦克的区域
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }

                        }

                    }
                }
                break;
            case 1://右
                //遍历所有坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //如果不是当前坦克,则判断
                    if (this != enemyTank) {
                        //如果另一个坦克的方向是上或者下
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //判断当前坦克的右上角是否落在另一个坦克的区域
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }

                            //判断当前坦克的右下角是否落在另一个坦克的区域
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }

                        //如果另一个坦克的方向是右或者左
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //判断当前坦克的右上角是否落在另一个坦克的区域
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }

                            //判断当前坦克的右下角是否落在另一个坦克的区域
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }

                        }

                    }
                }
                break;
            case 2://下
                //遍历所有坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //如果不是当前坦克,则判断
                    if (this != enemyTank) {
                        //如果另一个坦克的方向是上或者下
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //判断当前坦克的左下角是否落在另一个坦克的区域
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }

                            //判断当前坦克的右下角是否落在另一个坦克的区域
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }

                        //如果另一个坦克的方向是右或者左
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //判断当前坦克的左下角是否落在另一个坦克的区域
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }

                            //判断当前坦克的右下角是否落在另一个坦克的区域
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }

                        }

                    }
                }
                break;
            case 3://左
                //遍历所有坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //如果不是当前坦克,则判断
                    if (this != enemyTank) {
                        //如果另一个坦克的方向是上或者下
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //判断当前坦克的左上角是否落在另一个坦克的区域
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }

                            //判断当前坦克的左下角是否落在另一个坦克的区域
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }

                        //如果另一个坦克的方向是右或者左
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //判断当前坦克的左上角是否落在另一个坦克的区域
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }

                            //判断当前坦克的左下角是否落在另一个坦克的区域
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }

                        }

                    }
                }
                break;
        }
        return false;
    }
}


